package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.dao.BaseProdInfoMapper;
import cc.rengu.igas.bomp.common.dao.impl.BaseProdInfoMapperImpl;
import cc.rengu.igas.bomp.common.entity.BaseProdInfo;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.bean.BaseProdInfoBean;
import cc.rengu.igas.bomp.facade.request.BaseProdInfoListQueryRequest;
import cc.rengu.igas.bomp.facade.response.BaseProdInfoListQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 基础产品列表查询服务
 * Created by 王成 on 2020/3/6
 */
public class BaseProdInfoListQueryService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        BaseProdInfoListQueryRequest baseProdInfoListQueryRequest = new BaseProdInfoListQueryRequest();
        ConvertUtil.convertOutput(baseProdInfoListQueryRequest);
        return baseProdInfoListQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        BaseProdInfoListQueryRequest baseProdInfoListQueryRequest = (BaseProdInfoListQueryRequest) request;
        BaseProdInfoListQueryResponse baseProdInfoListQueryResponse = new BaseProdInfoListQueryResponse();
        BizResponse<BaseProdInfoListQueryResponse> bizResponse = new BizResponse<>();
        BeanUtil.beanCopy(baseProdInfoListQueryRequest, baseProdInfoListQueryResponse);
        /* 根据法人行标识查询法人行下所有基础产品信息 */
        BaseProdInfoMapper baseProdInfoMapper = new BaseProdInfoMapperImpl();
        List<BaseProdInfo> baseProdInfoList = baseProdInfoMapper.selectBaseProdInfoByInstId(baseProdInfoListQueryRequest.getHeader().getInstId());
        if (null == baseProdInfoList || baseProdInfoList.isEmpty()) {
            rglog.error("法人行<{}>还未配置基础产品信息！", baseProdInfoListQueryRequest.getHeader().getInstId());
            baseProdInfoListQueryResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
            baseProdInfoListQueryResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespDesc());
            bizResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
            bizResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespDesc());
            bizResponse.setResult(baseProdInfoListQueryResponse);
            return bizResponse;
        } else {
            List<BaseProdInfoBean> baseProdInfoBeans = new ArrayList<>();
            baseProdInfoList.forEach(item -> convBaseProdInfo(item, baseProdInfoBeans));
            baseProdInfoListQueryResponse.setBaseProdInfoList(baseProdInfoBeans);
        }
        /* 按照接口转换基础产品信息并响应给请求方 */
        baseProdInfoListQueryResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        baseProdInfoListQueryResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(baseProdInfoListQueryResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        BaseProdInfoListQueryResponse baseProdInfoListQueryResponse = (BaseProdInfoListQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(baseProdInfoListQueryResponse);
    }

    /**
     * 将数据库表结构对象转换成接口对象
     *
     * @param baseProdInfo      数据库表结构对象
     * @param baseProdInfoBeans 接口对象
     */
    private void convBaseProdInfo(BaseProdInfo baseProdInfo, List<BaseProdInfoBean> baseProdInfoBeans) {
        BaseProdInfoBean baseProdInfoBean = new BaseProdInfoBean();
        BeanUtil.beanCopy(baseProdInfo, baseProdInfoBean);
        baseProdInfoBeans.add(baseProdInfoBean);
    }
}
